package ViewListener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import Model.Connection;
import View.Client;
import View.LoginView;

public class LoginViewActionListener implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String ac = e.getActionCommand();		
		switch(ac)
		{
		case "Login":
			
			//MainController.CloseView(MainController.LOGIN_VIEW_NAME);
			//MainController.OpenMainView();
			LoginView.GetInformation();
			Client cli = new Client();		
			File file = new File("config");		
			
			try {
				file.delete();
				file.createNewFile();
				
				if(Connection.IsSaveUserAndPass)
				{
					FileOutputStream fos = new FileOutputStream("config");
					ObjectOutputStream oos = new ObjectOutputStream(fos); 					
					oos.write(1);
					oos.writeUTF(LoginView.user);
					oos.writeUTF(LoginView.password);
					oos.flush();
					oos.close();
					fos.close();
				}
				else
				{
					FileOutputStream fos = new FileOutputStream("config");
					ObjectOutputStream oos = new ObjectOutputStream(fos); 	
					oos.write(0);
					oos.flush();
					oos.close();
					fos.close();
				}
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}	
			break;
		case "Close":
			System.exit(0);
			break;
		}
	}

}
